package com.gitee.wsl.security.rbac

import com.gitee.wsl.struct.category.CategoryItemWarp


open class User(var name: String){}

open class UserGroup(name: String="",val userList: MutableList<User> = mutableListOf()): User(name){

    companion object{
        val DEFAULT= UserGroup("")
    }
}

class UserCategory(data: UserGroup): CategoryItemWarp(data){

    fun contains(user: User):Boolean = (data as UserGroup).userList.contains(user)

    companion object{
        val ROOT_USER_GROUP = UserCategory(UserGroup.DEFAULT)
    }

}

class UserRole(val user: User, val listRole: MutableList<Role>){}



/**
 * Dsl  相关类
 */

interface UserGroupScope{
    var name:String

    fun user(vararg name: String)

    fun userGroup(userGroupHandler : UserGroupScope.()->Unit)
}

class UserGroupScopeImp(val leave:Int=0): UserGroupScope {
    var userGroup: UserGroup?=null
    var userList:List<String>?=null
    var subUserGroupList = mutableListOf<UserGroupScopeImp>()

    override var name: String
        get() = userGroup?.name?:""
        set(value) {
            userGroup = UserGroup(value)
        }

    override fun user(vararg name: String) {
        userList = name.asList()

    }

    override fun userGroup(userGroupHandler: UserGroupScope.() -> Unit) {
        val subUserGroup = UserGroupScopeImp(leave+1)
        userGroupHandler(subUserGroup)
        subUserGroupList.add(subUserGroup)
    }
}